package com.hucc.api.controller;


import com.alibaba.fastjson.JSON;
import com.hucc.api.domain.exceptions.BusinessException;
import com.hucc.api.domain.req.ProjectReq;
import com.hucc.api.domain.res.ApiResult;
import com.hucc.api.domain.res.ProjectRes;
import com.hucc.api.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author huguocai
 */
@Slf4j
@Api("项目相关接口")
@RestController
@RequestMapping("/project")
public class ProjectContoller {
    @Autowired
    ProjectService projectService;

    /**
     * /project/add
     * @param projectReq
     */
    @ApiOperation("添加项目")
    @PostMapping("add")
    public ApiResult<Void> add(@RequestBody ProjectReq projectReq) {
        try {
            projectService.add(projectReq);
            return ApiResult.success();
        } catch (BusinessException e) {
            log.error("add project error, projectReq: {}", JSON.toJSONString(projectReq), e);
            return ApiResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error("add project error, projectReq: {}", JSON.toJSONString(projectReq), e);
            return ApiResult.fail("添加项目失败");
        }
    }


    @ApiOperation("查询项目列表")
    @PostMapping("list")
    public ApiResult<List<ProjectRes>> list() {
        try {
            List<ProjectRes> projectResList = projectService.list();
            return ApiResult.success(projectResList);
        } catch (BusinessException e) {
            log.error("list project error", e);
            return ApiResult.fail(e.getMessage());
        } catch (Exception e) {
            log.error("list project error", e);
            return ApiResult.fail("查询项目列表失败");
        }
    }
}
